package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import negocio.Alumno;
import negocio.AlumnoControlador;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.SwingConstants;



public class AlumnosCrear extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JLabel lblProvincia;
	private JPanel panel;
	private JButton btnCancelar;
	private JButton btnAceptar;
	JLabel error;

//	/**
//	 * Launch the application.
//	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					AlumnosCrear frame = new AlumnosCrear();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public AlumnosCrear(Principal padre) {
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				padre.nullAlumnosCrear();
			}
		});
		
		setTitle("Crear Alumno");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 553, 272);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Datos de Alumno", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(16, 19, 504, 136);
		contentPane.add(panel);
		panel.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(60, 16, 183, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(60, 47, 183, 20);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(60, 78, 183, 20);
		panel.add(textField_2);
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(315, 16, 183, 20);
		panel.add(textField_3);
		textField_3.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setBounds(315, 47, 183, 20);
		panel.add(textField_4);
		textField_4.setColumns(10);
		
		textField_5 = new JTextField();
		textField_5.setBounds(315, 78, 183, 20);
		panel.add(textField_5);
		textField_5.setColumns(10);
		
		textField_6 = new JTextField();
		textField_6.setBounds(60, 109, 183, 20);
		panel.add(textField_6);
		textField_6.setColumns(10);
		
		textField_7 = new JTextField();
		textField_7.setBounds(315, 109, 183, 20);
		panel.add(textField_7);
		textField_7.setColumns(10);
		
		textField.setText("");
		textField_1.setText("");
		textField_2.setText("");
		textField_3.setText("");
		textField_4.setText("");
		textField_5.setText("");
		textField_6.setText("");
		textField_7.setText("");
		
		JLabel lblDni = new JLabel("DNI: ");
		lblDni.setBounds(6, 19, 34, 14);
		panel.add(lblDni);
		
		JLabel lblNombre = new JLabel("Nombre: ");
		lblNombre.setBounds(6, 50, 46, 14);
		panel.add(lblNombre);
		
		JLabel lblApellido = new JLabel("Apellido: ");
		lblApellido.setBounds(6, 81, 46, 14);
		panel.add(lblApellido);
		
		JLabel lblTelefono = new JLabel("Telefono: ");
		lblTelefono.setBounds(253, 19, 52, 14);
		panel.add(lblTelefono);
		
		JLabel lblMail = new JLabel("Mail: ");
		lblMail.setBounds(253, 50, 34, 14);
		panel.add(lblMail);
		
		JLabel lblDireccion = new JLabel("Direccion: ");
		lblDireccion.setBounds(253, 81, 52, 14);
		panel.add(lblDireccion);
		
		JLabel lblPartido = new JLabel("Partido: ");
		lblPartido.setBounds(6, 112, 46, 14);
		panel.add(lblPartido);
		
		lblProvincia = new JLabel("Provincia: ");
		lblProvincia.setBounds(253, 112, 52, 14);
		panel.add(lblProvincia);
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				padre.nullAlumnosCrear();
				dispose();
			}
		});
		btnCancelar.setBounds(438, 200, 89, 23);
		contentPane.add(btnCancelar);
		
		btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Validador validador = new Validador();
				if(validador.validarDNI(textField))
				{
					if(validador.validarTelefono(textField_3))
					{
						if(validador.validarNombreOApellido(textField_1))
						{
							if(validador.validarNombreOApellido(textField_2))
							{
								if(validador.validarMail(textField_4))
								{
									if(textField_5.getText().trim().length()>0)
									{
										if(textField_6.getText().trim().length()>0)
										{
											if(textField_7.getText().trim().length()>0)
											{
												Alumno alumno = new Alumno(textField.getText(), textField_1.getText(), textField_2.getText(), textField_3.getText(), textField_4.getText(), textField_5.getText(), textField_6.getText(), textField_7.getText());
												AlumnoControlador.guardar(alumno);
												padre.nullAlumnosCrear();
												dispose();
											}
											else
											{
												error.setText("Debe completar el campo PROVINCIA");
												error.setVisible(true);
											}
										}
										else
										{
											error.setText("Debe completar el campo PARTIDO");
											error.setVisible(true);
										}
									}
									else
									{
										error.setText("Debe completar el campo DIRECCION");
										error.setVisible(true);
									}
								}
								else
								{
									error.setText("El mail ingresado no es valido");
									error.setVisible(true);
								}
							}
							else
							{
								error.setText("El apellido ingresado no es valido");
								error.setVisible(true);
							}
						}
						else
						{
							error.setText("El nombre ingresado no es valido");
							error.setVisible(true);
						}
					}
					else
					{
						error.setText("El telefono ingresado no es valido");
						error.setVisible(true);
					}
				}
				else
				{
					error.setText("El DNI ingresado no es valido");
					error.setVisible(true);
				}
				
			}
		});
		btnAceptar.setBounds(339, 200, 89, 23);
		contentPane.add(btnAceptar);
		
		error = new JLabel("New label");
		error.setVisible(false);
		error.setHorizontalAlignment(SwingConstants.CENTER);
		error.setForeground(Color.RED);
		error.setBounds(16, 166, 504, 14);
		contentPane.add(error);
	}
}
